# -*- coding: utf-8 -*-
"""
Created on Sat Sep 17 11:10:57 2022

@author: 123
"""

#计算相关系数
fin_data_index_df.to_csv("fin_data_index_df.csv")

#fin_data_index_df.to_excel('D:\/'+fin_data_index_df.xlsx,index=False)


data = fin_data_index_df.iloc[:, 1:17]
data_corr=data.corr()
data_corr.to_csv("data_corr.csv")

#df.to_csv('E:/xx/xx/' + new_file_name, index=False)
#data_corr.to_csv("data_corr.csv")
#data_corr.to_csv('D:\/'+data_corr.csv,index=False)




data = fin_data_index_df.iloc[:, 1:17]

fin_data_index_df.columns.tolist()
# 通过特征得分 重要性来筛选变量 
X,y=(fin_data_index_df.iloc[:,1:]).iloc[:,:-1],fin_data_index_df['Risk_Flag']


import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import RidgeCV

ridge = RidgeCV(alphas=np.logspace(-1, 1, num=15)).fit(X, y)
importance = np.abs(ridge.coef_)
#feature_names = np.array(['Income','Age','Experience','CURRENT_JOB_YRS','CURRENT_HOUSE_YRS','Married/Single_single','House_Ownership_owned','House_Ownership_rented','Car_Ownership_yes','Profession_is_handle_affairs_personnel','Profession_is_person_in_charge_of_enterprises_and_institutions','Profession_is_production_transport_worker','Profession_is_professiona_skill_personnel','STATE_high','STATE_low','STATE_middle'])
feature_names = np.array(['x0','x1','x2','x3','x4','x5','x6','x7','x8','x9','x10','x11','x12','x13','x14','x15'])
plt.bar(height=importance, x=feature_names)
plt.xticks(rotation=-45)
plt.title("Feature importances via coefficients")
plt.show()



from sklearn.feature_selection import SelectFromModel
from time import time

threshold = np.sort(importance)[-15]+0.0000001

tic = time()
sfm = SelectFromModel(ridge, threshold=threshold).fit(X, y)
toc = time()
print(f"Features selected by SelectFromModel: {feature_names[sfm.get_support()]}")
print(f"Done in {toc - tic:.3f}s")

